package com.yn.club.auth.domain.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yn.club.common.enums.IsDeletedFlagEnum;
import com.yn.club.common.util.PageReq;
import com.yn.club.common.util.PageResp;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.yn.club.auth.domain.entity.AuthRoleDO;
import com.yn.club.auth.infra.basic.entity.AuthRole;
import com.yn.club.auth.domain.service.AuthRoleDomainService;
import com.yn.club.auth.infra.basic.service.AuthRoleService;
import com.yn.club.auth.domain.convert.AuthRoleDomainConverter;


/**
 * (AuthRole)表服务接口实现类
 *
 * @author YN
 * @since 2025-01-22 10:07:43
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class AuthRoleDomainServiceImpl implements AuthRoleDomainService {

  private final AuthRoleService authRoleService;
  private final AuthRoleDomainConverter authRoleDomainConverter;


  /**
   * 分页
   */
  @Override
  public PageResp<AuthRoleDO> page(PageReq pageReq, AuthRoleDO query){
    QueryWrapper<AuthRole> querywrapper = new QueryWrapper<AuthRole>()
        .eq(query.getId() != null, "id", query.getId())
        .orderByDesc("id");
        
    Page<AuthRole> page = authRoleService.page(
      new Page<>(pageReq.getPageIndex(),pageReq.getPageSize()),querywrapper);
    List<AuthRole> list = page.getRecords();  
    return PageResp.of(page,authRoleDomainConverter.convertEntityToDo(list));
  }

  /**
   * 新增数据
   */
  @Override
  public Boolean insert(AuthRoleDO obj){
    AuthRole authRole = authRoleDomainConverter.convertDoToEntity(obj);
    authRole.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
    authRole.setCreatedTime(LocalDateTime.now());
    authRole.setUpdateTime(LocalDateTime.now());
    return authRoleService.save(authRole);
  };


  /**
   * 更新数据
   */
  @Override
  public Boolean updateById(AuthRoleDO obj){
    AuthRole authRole = authRoleDomainConverter.convertDoToEntity(obj);
    authRole.setUpdateTime(LocalDateTime.now());
    return authRoleService.updateById(authRole);
  };

  /**
   * 批量删除
   */
  @Override
  public Boolean delete(List<Integer> ids){
    UpdateWrapper<AuthRole> set = new UpdateWrapper<AuthRole>()
        .in("id", ids)
        .set("is_deleted", IsDeletedFlagEnum.DELETED.getCode());
    return authRoleService.update(set);
  };

  /**
   * 根据id查找
   */
  @Override
  public AuthRoleDO getById(Serializable id){
    AuthRole authRole = authRoleService.getById(id);
    return  authRoleDomainConverter.convertEntityToDo(authRole);
  };


}

